package com.panovue.dataextraction.reader;

import cn.edu.hfut.dmic.webcollector.model.CrawlDatum;
import cn.edu.hfut.dmic.webcollector.model.CrawlDatums;
import cn.edu.hfut.dmic.webcollector.model.Page;
import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.panovue.dataextraction.ReaderUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;

@Slf4j
public class SigmaReader extends AbstractReader {

    public SigmaReader() {
        super("sigmaaldrich", true);
    }

    public SigmaReader(String key) {
        super("sigmaaldrich", true);
        this.key = key.replace(" ", "-");

        baseUrl = "https://www.sigmaaldrich.cn/";
        baseDir += key + File.separator + "sigmaaldrich" + File.separator;

        // 设置线程数
        setThreads(5);
        addSeedAndReturn(baseUrl + "CN/zh/search/" + this.key + "?focus=products&page=1&perpage=30&sort=relevance&term=anti-cd8a%20antibody%20produced%20in%20rabbit&type=product_name").type("list");


        getConf().setExecuteInterval(3000);

        buildDriver(true);

        // 开始爬取
        try {
            start(20);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void visit(Page page, CrawlDatums next) {
        data.set("应用类型", "");
        data.set("种属来源", "");
        data.set("反应种属", "");
        data.set("克隆属性", "");
        data.set("着色样式", "");
        data.set("稀释倍数", "");
        data.set("染色例图，对应的样本信息， 酸碱修复条件", "");
        data.set("参考文献情况", "");
    }

    @MatchType(types = "list")
    public void list(Page page, CrawlDatums next) {
        String href = page.select(".MuiTable-root").first().select("td a").get(1).attr("href");
        next.addAndReturn(baseUrl + href).type("details");
    }

    @MatchType(types = "details")
    public void details(Page page, CrawlDatums next) {
        JSONArray productDetails = new JSONArray();
        JSONObject jsonObject = JSONUtil.parseObj((page.select("#__NEXT_DATA__").first().childNode(0).toString())).getJSONObject("props").getJSONObject("pageProps").getJSONObject("data").getJSONObject("getProductDetail");
        JSONArray attributes = jsonObject.getJSONArray("attributes");

        for (Object attribute : attributes) {
            JSONObject entries = JSONUtil.parseObj(attribute);
            productDetails.add(entries);
        }

        data.set("产品详情", productDetails);

        JSONArray imagesInfo = new JSONArray();

        JSONArray images = jsonObject.getJSONArray("images");
        for (Object image : images) {
            JSONObject entries = JSONUtil.parseObj(image);
            imagesInfo.add(entries);
            String largeUrl = entries.getStr("largeUrl");
            ReaderUtil.saveFile(baseUrl + largeUrl, baseDir + "images" + File.separator + largeUrl.substring(largeUrl.lastIndexOf("/") + 1));
        }
        data.set("图片", imagesInfo);


    }

    @MatchType(types = "literatures")
    public void literatures(Page page, CrawlDatums next) {
        JSONArray literatures = new JSONArray();
        for (Object object : page.select("")) {
            JSONObject jsonObject = JSONUtil.parseObj(object);
            String title = jsonObject.getStr("Title");
            String url = "https://pubmed.ncbi.nlm.nih.gov/" + jsonObject.getInt("PubmedID");
            jsonObject.set(title, url);
            literatures.add(jsonObject);
        }
        data.set("文献", literatures);
    }

    @MatchCode(codes = {301, 302, 307})
    public void retry(Page page, CrawlDatums next) {
        CrawlDatum crawlDatum = new CrawlDatum();
        crawlDatum.url(baseUrl + page.location());
        crawlDatum.type(page.matchType("list") ? "list" : "details");
        crawlDatum.key(crawlDatum.getExecuteTime() + page.url());
        next.addAndReturn(crawlDatum);
    }

    @Override
    public void afterStop() {
        if (driver != null) {
            driver.quit();
        }
        FileUtil.writeUtf8String(data.toString(), baseDir + File.separator + key + ".json");
        log.info("sigmaaldrich 数据爬取完成");
    }

    @Override
    public void execute(CrawlDatum datum, CrawlDatums next) throws Exception {
        super.execute(datum, next);
        log.info("url {} code {}", datum.url(), datum.code());
    }

    @Override
    public Reader buildReader(String key) {
        return new SigmaReader(key);
    }
}
